using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Perovich.GameObjects.Helpers
{
    internal static class RenderTargetManager
    {
        private static object stateObj = new object();
        private static Dictionary<object, RenderTarget2D> RenderTargets = new Dictionary<object, RenderTarget2D>();

        internal static RenderTarget2D GetRenderTarget(GraphicsDevice device, float width, float height, object requester)
        {
            //if (requester is GameState)
            //    requester = stateObj;
            if ((!RenderTargets.ContainsKey(requester)) || (RenderTargets[requester].Width < width) || (RenderTargets[requester].Height < height))
            {
                if ((width != 0) & (height != 0))
                {
                    try
                    {
                        if ((RenderTargets.ContainsKey(requester))&&(RenderTargets[requester] != null))
                        {
                            RenderTargets[requester].Dispose();
                        }
                        RenderTargets[requester] = new RenderTarget2D(device, (int)width, (int)height, false, SurfaceFormat.Color, DepthFormat.Depth16,0,RenderTargetUsage.DiscardContents);
                    }
                    catch (NullReferenceException)
                    { }
                }
                else
                {
                    RenderTargets[requester] = null;
                }
            }
            return RenderTargets[requester];
        }

    }
}
